<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>保质期检查</title>
  <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/static/admin/style.css}" media="all">
<!--  <script type="text/html" id="indexTp1">-->
<!--    {{a.LAY_TABLE_INDEX+1}}}-->
<!--  </script>-->
</head>
<body class="layui-layout-body">
  <div class="layui-layout-body">
    <div class="layui-fluid">
      <div class="layui-row layui-col-space5">
        <div class="layui-col-md12">
          <div class="layui-card">
            <div class="layui-card-header">药品信息列表</div>
            <div class="layui-card-body">
              <table class="layui-hide" id="main-table" lay-filter="main-table"></table>
              <script type="text/html" id="toolbarDemo">
                <span class="layui-inline" lay-event="add"><i class="layui-icon">&#xe654;</i></span>
                <span class="layui-inline" style="width: 240px">
                  <input class="layui-input" name="param" id="param" placeholder="根据药品名称查询"autocomplete="off">
                </span>
                <span class="layui-btn layui-btn-sm layui-bg-red" lay-event="querySearch">
                  <i class="layui-icon">&#xe615;</i>查询
                </span>
              </script>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script th:src="@{/static/layui/layui.js}"></script>
  <script>
    layui.use(['form','table','jquery','layer'],function () {
      var form = layui.form;
      var table = layui.table;
      var $ = layui.jquery;
      var layer = layui.layer;

      table.render({
        elem:"#main-table",
        height:650,
        url:"/drugInfo/page",
        method:"POST",
        page:true,
        id:"table-load",
        toolbar:"#toolbarDemo",
        cols:[
          [
          {field: 'id',title:'序号',width:60,align:'center'},
          {field:'name',title:'药品名称',align: 'center'},
          {field:'productdate',title:'创建时间',align: 'center',
            templet: "<div>{{layui.util.toDateString(d.productdate,'yyyy-MM-dd')}}</div>"},
          // {field:'createdate',title:'当前时间',align: 'center',
          //   templet: "<div>{{layui.util.toDateString(d.createdate,'yyyy-MM-dd')}}</div>"},
            {title: '是否过期',align: 'center',templet: isWarranty}
          ]
        ]
      });

      //监听头部工具栏
      table.on('toolbar(main-table)',function (obj) {
        switch (obj.event) {
          case 'querySearch':
            var param = $("#param").val();
            table.reload('table-load',{
              where:{
                param:param
              }
            });
            $('#param').val(param);
            break;
        }
      });
      
      //计算药品是否过期
      function isWarranty(d) {
        //生产日期
        var p_time = d.productdate;
        p_time=p_time.split('-');
        p_time=parseInt(p_time[0])*12+parseInt(p_time[1]);
        //获取当前时间
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth()+1;
        var now_year_month = year*12+month;
        var m = now_year_month - p_time - parseInt(d.warranty);
        if (m>=1){
          return "<span style='color:red'>已过期"+m+"个月</span>";
        }else if(m==0){
          return "<span style='color:blue'>保质期不满一个月</span>";
        }else{
          return "<span style='color:green'>未过期,剩余"+Math.abs(m)+"个月</span>";
        }
      }

    });
  </script>
</body>
</html>